package com.controller.sys;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.common.base.BaseController;
import com.common.util.LoginUtil;
import com.entity.sys.SysSafe;
import com.service.sys.SysSafeService;
import fuhua.com.anno.CheckPermission;
import fuhua.com.anno.Log;
import fuhua.com.util.ResultUtil;
import org.springframework.web.bind.annotation.*;


/**
 * 安全设置
 */
@RestController
@RequestMapping("/sys/safe")
public class SysSafeController extends BaseController<SysSafeService,SysSafe> {

    /**
     * 修改安全设置
     */
    @Log(title = "修改安全设置")
    @PostMapping("/update")
    @CheckPermission(perm = "system:safe:update")
    public ResultUtil update(@RequestBody SysSafe sysSafe){
        if (StrUtil.isBlank(sysSafe.getId())){
            sysSafe.setId(IdUtil.getSnowflakeNextIdStr());
            service.save(sysSafe);
        }else {
            service.updateById(sysSafe);
            String token = token();
            long expire = LoginUtil.getTimeOut(token);
            int i = sysSafe.getIdleTimeSetting();
            //修改token的过期时间
            if (expire > 0 && i==0){
                LoginUtil.updateTimeOut(token,-1);
            }else if (expire < 0 && i!=0){
                LoginUtil.updateTimeOut(token,3600);
            }
        }
        return ResultUtil.success();
    }

    @GetMapping("/getSafeSet")
    @CheckPermission(perm = "system:safe:view")
    public ResultUtil getSafeSet(){
        return ResultUtil.success(service.list().get(0));
    }
}
